package work.tree;


import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import work.TreeNode;

public class Problem98 {

    //前xu遍历
    public boolean isValidBST7(TreeNode root) {

        long l = Long.MIN_VALUE;
        long r = Long.MAX_VALUE;

        return DST(root, l, r);
    }

    public boolean DST(TreeNode root, long l, long r) {
        if (root == null) return true;
        boolean a = l < root.val && r > root.val;
        boolean b = DST(root.left, l, root.val);
        boolean c = DST(root.right, root.val, r);
        return a && b && c;
    }

    int pre = Integer.MIN_VALUE;

    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        boolean a = isValidBST(root.left);
        if (root.val <= pre) return false;
        pre = root.val;
        boolean b = isValidBST(root.right);
        return a && b;
    }


}
